# SystemTrayIcon class - Implements system tray icon for Entertainer.
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import sys
import os

try:
 	import pygtk
  	pygtk.require("2.4")
except:
  	pass
try:
	import gtk
  	import gtk.glade
except:
	sys.exit(1)

from backend.core.message_type_priority import MessageType, MessagePriority
from backend.core.message_handler import MessageHandler

from utils.log_viewer import LogViewer
from utils.preferences_dialog import PreferencesDialog
from utils.content_management_dialog import ContentManagementDialog
	
class SystemTrayIcon(MessageHandler):
    """Implements system tray icon for entertainer."""
    
    # Icon states
    NORMAL_ICON = 0
    RECORD_ICON = 1 

    def __init__(self):
        """Create system tray icon and pop-up menu for it."""
        self.popup = None
        self.message_bus_proxy = None
        self.icon_widget = gtk.StatusIcon()
        self.icon_widget.set_tooltip("Entertainer Server")
        self.icon_widget.set_from_icon_name("video-television")
        
        # Load glade files
        try:
            self.menu_widgets = gtk.glade.XML("utils/glade/system_tray_icon_menu.glade")
        except:
            sys.exit(1)
        # Bind menu signals
        callback_dic = {"on_menuitem_frontend_activate" : self.on_menuitem_frontend_activate,
                        "on_menuitem_preferences_activate" : self.on_menuitem_preferences_activate,
                        "on_menuitem_content_management_activate" : self.on_menuitem_content_management_activate,
                        "on_menuitem_log_viewer_activate" : self.on_menuitem_log_viewer_activate,
                        "on_menuitem_quit_activate" : self.on_menuitem_quit_activate}
        self.menu_widgets.signal_autoconnect(callback_dic)
        self.popup = self.menu_widgets.get_widget("SystemTrayIconMenu")
        self.icon_widget.connect('activate', self.systray_icon_activated)
        self.icon_widget.connect('popup-menu', self.open_popup_menu)
   
    def setMessageBusProxy(self, proxy):
        self.message_bus_proxy = proxy
        
    def showIcon(self):
        """Show icon in system tray"""
        self.icon_widget.set_visible(True)
    
    def hideIcon(self):
        """Hide icon from system tray"""
        self.icon_widget.set_visible(False)
        
    def changeIcon(self, icon_state):
        """Change displayd icon. RECORD_ICON is used when recording TV."""
        gtk.gdk.threads_enter()
        if icon_state == self.__NORMAL_ICON:
            self.icon_widget.set_from_icon_name("video-television")
            self.icon_widget.set_tooltip("Entertainer Server")
        elif icon_state == self.__RECORD_ICON:
            self.icon_widget.set_from_icon_name("gtk-media-record")
            self.icon_widget.set_tooltip("Entertainer is recording TV")
        gtk.gdk.threads_leave()
        
    def systray_icon_activated(self, widget, data= None):
        """Open frontend when system tray icon is clicked"""
        #FIXME: Execute frontend here if not running. Show if running
        pass
        
    def open_popup_menu(self, widget, button, time, data = None):
        """Display pop-up menu when system tray icon is clicked"""
        self.popup.show_all()
        self.popup.popup(None, None, None, 3, time)
    
    def on_menuitem_frontend_activate(self, widget):
        #FIXME: Execute frontend here if not running. Show if running
        pass
        
    def on_menuitem_preferences_activate(self, widget):
        """Executes preferences-tool."""
        PreferencesDialog(False)
        
    def on_menuitem_content_management_activate(self, widget):
        """Executes content-management-tool"""
        ContentManagementDialog(False)
        
    def on_menuitem_log_viewer_activate(self, widget):
        """Display log viewer dialog"""
        LogViewer(False)
        
    def on_menuitem_quit_activate(self, widget):
        """Show confirmation dialog and quit backend"""
        self.message_bus_proxy.disconnectFromMessageBus()
        gtk.gdk.threads_enter()
        gtk.main_quit()
        gtk.gdk.threads_leave()
    
    # Implement MessageHandler interface
    def handleMessage(self, message):
        """Change system tray icon when recording. Implements MessageHandler"""
        if message.get_type() == MessageType.RECORDING_STARTED:            
            self.changeIcon(self.__class__.RECORD_ICON)
        elif message.get_type() == MessageType.RECORDING_STOPPED:
            self.changeIcon(self.__class__.NORMAL_ICON)
               
    def getName(self):
        return "System tray icon"
